package com.weishi.project.training.controller;

import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.framework.web.page.TableDataInfo;
import com.weishi.project.api.ret.AnnualPlanRet;
import com.weishi.project.api.ret.NaturalYearRet;
import com.weishi.project.api.ret.WeekTreeRet;
import com.weishi.project.training.domain.AnnualPlan;
import com.weishi.project.training.domain.TrainingWeek;
import com.weishi.project.training.service.IAnnualPlanService;
import com.weishi.project.training.service.INaturalYearService;
import com.weishi.project.training.service.ITrainingWeekService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * annualPlanController
 */
@Api(value = "训练计划",description = "训练计划")
@RestController
@RequestMapping("/training/annualPlan")
public class AnnualPlanController extends BaseController {

    @Autowired
    private IAnnualPlanService annualPlanService;

    @Autowired
    private INaturalYearService naturalYearService;

    @Autowired
    private ITrainingWeekService trainingWeekService;

    /**
     * 查询训练计划列表
     */
    //@PreAuthorize("@ss.hasPermi('training:annualPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(AnnualPlan annualPlan) {

        startPage();
        List<AnnualPlan> list = annualPlanService.selectAnnualPlanList(annualPlan);
        return getDataTable(list);
    }

    /**
     * 导出训练计划列表
     */
    //@PreAuthorize("@ss.hasPermi('training:annualPlan:export')")
    @Log(title = "annualPlan", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AnnualPlan annualPlan) {
        List<AnnualPlan> list = annualPlanService.selectAnnualPlanList(annualPlan);
        ExcelUtil<AnnualPlan> util = new ExcelUtil<AnnualPlan>(AnnualPlan.class);
        return util.exportExcel(list, "annualPlan");
    }

    /**
     * 获取训练计划详细信息
     */
    //@PreAuthorize("@ss.hasPermi('training:annualPlan:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(annualPlanService.getAnnualPlanById(id));
    }

    /**
     * 新增训练计划
     */
    //@PreAuthorize("@ss.hasPermi('training:annualPlan:add')")
    @Log(title = "annualPlan", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AnnualPlan annualPlan) {
        annualPlan.setCtime(new Date());
        annualPlanService.save(annualPlan);
        //同步周信息
        trainingWeekService.saveTrainingWeek(annualPlan,annualPlan.getId());
        return AjaxResult.success();
    }

    /**
     * 修改训练计划
     */
    //@PreAuthorize("@ss.hasPermi('training:annualPlan:edit')")
    @Log(title = "annualPlan", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AnnualPlan annualPlan) {
        annualPlan.setLtime(new Date());
        annualPlanService.updateAnnualPlanById(annualPlan);
        return AjaxResult.success();
    }

    /**
     * 删除训练计划
     */
    //@PreAuthorize("@ss.hasPermi('training:annualPlan:remove')")
    @Log(title = "annualPlan", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        annualPlanService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取年度下拉数据")
    @GetMapping("/naturalYearTree")
    public AjaxResult naturalYearTree() {
        List<NaturalYearRet> list = naturalYearService.selectNaturalYearRetList(true);
        List<WeekTreeRet> ret = changeTree(list);
        return AjaxResult.success(ret);
    }

    private List<WeekTreeRet> changeTree(List<NaturalYearRet> list) {
        List<WeekTreeRet> weekTreeRets = new ArrayList<>();
        for(NaturalYearRet naturalYearRet : list){
            WeekTreeRet weekTreeRet1 = new WeekTreeRet();
            weekTreeRet1.setId(naturalYearRet.getId());
            weekTreeRet1.setName(naturalYearRet.getName());
            List<WeekTreeRet> weekTreeRets1 = new ArrayList<>();
            weekTreeRet1.setChildren(weekTreeRets1);
            List<AnnualPlanRet> annualPlans = naturalYearRet.getAnnualPlans();
            for(AnnualPlanRet annualPlanRet : annualPlans){
                WeekTreeRet weekTreeRet2 = new WeekTreeRet();
                weekTreeRet2.setId(annualPlanRet.getId());
                weekTreeRet2.setName(annualPlanRet.getCycleName());
                weekTreeRets1.add(weekTreeRet2);
                List<TrainingWeek> trainingWeeks = annualPlanRet.getTrainingWeeks();
                List<WeekTreeRet> weekTreeRets2 =  new ArrayList<>();
                weekTreeRet2.setChildren(weekTreeRets2);
                for(TrainingWeek trainingWeek : trainingWeeks){
                    WeekTreeRet weekTreeRet3 = new WeekTreeRet();
                    weekTreeRet3.setId(trainingWeek.getId());
                    weekTreeRet3.setName("第"+ trainingWeek.getWeek() + "周");
                    weekTreeRets2.add(weekTreeRet3);
                }
            }

            weekTreeRets.add(weekTreeRet1);
        }
        return weekTreeRets;
    }

}